<?php

namespace App\Model;


use Illuminate\Http\Request;

class KkBalanceStatistics extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'kk_balance_statistics';
    public $timestamps = false;
    protected $fillable = [ 'gift_kk', 'room_kk', 'withdraw_kk', 'exchange_kk', 'other_kk', 'grand_kk', 'grand_consume', 'now_kk', 'created_date'];
    
    public function getGiftKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getRoomKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getWithdrawKkAttribute($value){
        $value *= -1;
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getSystemKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getExchangeKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getOtherKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getGrandKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getGrandConsumeAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getNowKkAttribute($value){
        if(!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }
    
    public function getList(Request $request){
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $begin_date = $request->input('begin_date');
        $end_date = $request->input('end_date');
        
        $query = new self();
        
        if($begin_date && $end_date) $query = $query->whereBetween('created_date',[ $begin_date, $end_date ]);
        if($begin_date && !$end_date) $query = $query->where('created_date', '>=', $begin_date);
        if(!$begin_date && $end_date) $query = $query->where('created_date', '<=', $end_date);
        
        return $query->orderBy('id','DESC')->paginate($limit,['*'],'page',$page);
    }
}
